var express = require('express');
var localStorage = require('node-localstorage')
var router = express.Router();
var dbcomm = require('../util/dbcomm');
var db = require('../util/db');
var moment = require('moment');
var multer  = require('multer');//用express的第三方中间件 multer 实现文件上传功能。
var now = moment().format('YYYY年MM月DD日') ;//日期
const bodyParser = require('body-parser');
var upload = multer({dest:'./templates/BackStage/image'});

//处理页面数据 
router.use(bodyParser.urlencoded({ extended: true }));
router.use(bodyParser.json());

router.post('/selectRole',function(req,res){
    var para = req.body;
    var sql = '';
    sql = "SELECT * FROM sys_roleinformation ";
    db.page(sql,para,function(data){
        res.send(data);
    });
});

router.post('/deleteRole',function(req,res){
    var para = req.body;
    var sql='';
    sql ="delete from sys_roleinformation where "+para.primaryKey+" = '"+para.primaryKeyName+"'";
    db.delete(sql,para,function(data){
        res.send(data);
    });
});

router.post('/insertRole',upload.array('IMAGE',9),function(req,res){
    var para = req.body;
    var arr = {filename:['image']};
    var file=req.files;
    if(file){
        file.forEach(function(items,i){
            console.log("name["+i+"]:"+arr.fileName[i]);
            name=items.originalname;
            nameArray=name.split('');
            var nameMime=[];
            l=nameArray.pop();
            nameMime.unshift(l);
            while(nameArray.length!=0&&l!='.'){
                l=nameArray.pop();
                nameMime.unshift(l);
            }
            Mime=nameMime.join('');
            fs.renameSync('./templates/BackStage/image/'+items.filename,'./templates/BackStage/image/'+items.filename+Mime);
            para[arr.fileName[i]] = '/BackStage/image/'+items.filename+Mime;
            console.log(arr.fileName[i])
        })
    }
    var sql='';
    sql ="INSERT INTO sys_roleinformation ";
    db.insert(sql,para,function(data){
        if(data.result == 'succeed'){
            res.redirect(para.returnUrl);
        }
    });
});

router.post('/updateRole',upload.array('IMAGE',9),function(req,res){
    var para = req.body;
    console.log(para);
    var arr = req.query;
    var file=req.files;
    if(file){
        file.forEach(function(items,i){
            console.log("name["+i+"]:"+arr.fileName[i]);
            name=items.originalname;
            nameArray=name.split('');
            var nameMime=[];
            l=nameArray.pop();
            nameMime.unshift(l);
            while(nameArray.length!=0&&l!='.'){
                l=nameArray.pop();
                nameMime.unshift(l);
            }
            Mime=nameMime.join('');
            fs.renameSync('./templates/BackStage/image/'+items.filename,'./templates/BackStage/image/'+items.filename+Mime);
            para[arr.fileName[i]] = '/BackStage/image/'+items.filename+Mime;
        })
    }
    var sql='';
    sql ="update sys_roleinformation SET ";
    db.update(sql,para,function(data){
        if(data.result == 'succeed'){
            res.redirect(para.returnUrl);
        }
    });
});

module.exports=router;